<template>
  <el-breadcrumb separator="/" class="mb-2 mt-1" style="vertical-align: bottom">
    <el-breadcrumb-item>
      <div class="flex align-center">
        <el-icon :size="18"> <Setting /> </el-icon><span class="pl">設定</span>
      </div>
    </el-breadcrumb-item>
    <el-breadcrumb-item>收款管理</el-breadcrumb-item>
  </el-breadcrumb>
  <el-card class="box-card" shadow="hover">
    <template #header>
      <div class="flex justify-between flex-wrap">
        <el-space wrap :size="10" class="mt">
          <el-input
            v-model="params.rule_name"
            style="width: 150px"
            placeholder="规则名称"
          />
          <el-button :icon="Search" plain @click="search">查詢</el-button>
        </el-space>
        <el-space wrap :size="10" class="mt">
          <el-button type="primary" plain>增加</el-button>
        </el-space>
      </div>
    </template>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="rule_id" label="ID" width="80" />
      <el-table-column prop="rule_name" label="规则名称" />
      <el-table-column label="匹配规则" width="100">
        <template #default="scope">
        <span v-if="scope.row.match_type==1">回复所有内容</span>
        <span v-else>随机回复一条内容</span>
         </template>
      </el-table-column>
      <el-table-column prop="keywords" label="关键字" />
      <el-table-column prop="msg_info" label="回复内容">
        <template #default="scope" width="250">
        <el-text v-if="scope.row.msg_info[0].msg_type=='text'" line-clamp="2">{{scope.row.msg_info[0].content}}</el-text>
        <div v-else>materialT_{{scope.row.msg_info[0].material_type}}：<el-link type="primary" @click="linkClick(scope.row.msg_info[0].material_type)">{{scope.row.msg_info[0].material_name}}</el-link></div>
         <div class="font-sm">回复内容个数：{{scope.row.msg_info.length}}</div>
        </template>
      </el-table-column >
      <el-table-column label="有效时间" width="120">
          <template #default="scope">
          <el-space direction="vertical" :size="1">
        <span>{{scope.row.start_date}}</span>
        <span>{{scope.row.end_date}}</span>
        </el-space>
         </template>
      </el-table-column>
      <el-table-column label="标签">
        <template #default="scope">
          <el-space wrap>
            <el-tag v-for="(item, index) in scope.row.tags">{{ item }}</el-tag>
          </el-space>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="scope">
          <el-button
            link
            size="small"
            type="primary"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            link
            size="small"
            type="danger"
            @click="handleEdit(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div v-if="total > params.page_size" class="mt-2 flex justify-center">
      <el-pagination
        v-model:current-page="params.current_page"
        v-model:page-size="params.page_size"
        :page-sizes="[50, 100, 200]"
        :small="false"
        :disabled="false"
        :background="true"
        layout="total, sizes, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>
</template>
<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { Search, Setting } from "@element-plus/icons-vue";
import { $keyList } from "@api/setting";
import router from "@/router/";
import { useLocaleStore } from "@/store/locales";
const useLocale = useLocaleStore();
const match_arr = [
  {
    id: 1,
    name: "完全匹配",
  },
  {
    id: 2,
    name: "模糊匹配",
  },
];
const reply_arr = [
  {
    id: 1,
    name: "回复所有内容",
  },
  {
    id: 2,
    name: "随机回复一条内容",
  },
];
let params = ref({
  current_page: 1,
  page_size: 50,
  rule_name: "",
});
const total = ref(0);
const tableData = ref([]);
onMounted(() => {
  getData();
});
const getData = async () => {
  let res = await $keyList(params.value);
  if (res.code == 0) {
    tableData.value = res.data.table;
    total.value = res.data.total;
  }
};
const handleSizeChange = (val: number) => {
  params.value.page_size = val;
  getData();
  console.log(`${val} items per page`);
};
const handleCurrentChange = (val: number) => {
  params.value.current_page = val;
  getData();
  console.log(`current page: ${val}`);
};
const search = () => {
  console.log(params.value);
  getData();
};
const handleClick = (tab: TabsPaneContext, event: Event) => {
  console.log(params);
  getData();
};
const linkClick=(type)=>{
  console.log(type);
  useLocale.setActiveIndex("/setting/messageCont");
   router.push({
            path: "/setting/messageCont",
            query:{material_type:type}
   });

}
</script>

<style lang="scss" scoped></style>
